from datetime import *
import os
import time as T
import pyautogui as P

tmNow = datetime.now()

def off():
    global shtdownTime_list
    L = []
    with open('./off.txt', 'r')as F:
        for i in F:
            L.append(i.split('-'))
        print(f'{L}')
    shtdownTime_list=[]
    for i in L:
        j=datetime.combine(date.today(),time(int(i[0]),int(i[1]),int(i[2])))
        shtdownTime_list.append(j)

def ShutDown(T):
    tmNow = datetime.now()
    timedDelta = (T - tmNow).total_seconds()
    if timedDelta < -20:
        print(str(T)+'过时')
        return ('continue')
    if timedDelta < 50 and timedDelta > -8:
        os.system('shutdown -s -f -c 请及时在弹出的页面内选择 -t 80')
        print(str(T)+'到时')
        return ('break')
    else:
        print(str(T)+'没到')
        return ('continue')


if __name__ == '__main__':
    while True:
        off()
        for i in shtdownTime_list:
            if ShutDown(i) =='break' :
                flag='break'
                break
            else:
                flag='continue'
                continue
        if flag=='break':
            break
        T.sleep(5)
    while True:
        P.hotkey('win')
        P.hotkey('win', 'm')
        #userword=P.confirm(text='请选择关机时间:\n(1分钟未选择自动关机)',title='温馨提示',buttons=['3分钟后','6分钟后','重启','注销','关机'])
        userword = P.confirm(text='请选择关机时间:\n(1分钟未选择自动关机)',title='温馨提示',buttons=['5分钟后','10分钟后','关机'])
        if userword =='5分钟后':
            os.system('shutdown -a')
            for i in range(300, 0,-1):
                T.sleep(1)
                print(i)
            os.system('shutdown -s -f -t 80')
        elif userword =='10分钟后':
            os.system('shutdown -a')
            for i in range(600, 0,-1):
                T.sleep(1)
                print(i)
            os.system('shutdown -s -f -t 80')
        elif userword =='关机':
            os.system('shutdown -f -p')
            break
        elif userword =='注销':
            os.system('shutdown -a')
            os.system('shutdown -l -f -t 1')  # L
            break
        elif userword =='重启':
            os.system('shutdown -a')
            os.system('shutdown -r -f -t 1')  # R
            break
